package com.cxhc.demo1006.net;

import android.content.Context;

import com.cxhc.demo1006.MainService;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetClient {
    private static NetClient mNetClient;
    private static OkHttpClient client;
    private Context mContext;
    private NetClient(){}


    public static NetClient getInstance(){
        mNetClient = new NetClient();
        client = new OkHttpClient.Builder()
                .readTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(5, TimeUnit.SECONDS)
                .connectTimeout(5, TimeUnit.SECONDS)
                .addNetworkInterceptor(new HttpLoggingInterceptor())
                .retryOnConnectionFailure(true)
//                .cookieJar(null)
                .build();
        return mNetClient;
    }


    private static Retrofit retrofit;
    public static BaseService  initRetrofit(){
        retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(Constant.BASEURL)
                .addCallAdapterFactory( RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit.create(BaseService.class);
    }

    public static MainService createMainService(){
        retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(Constant.BASEURL)
                .addCallAdapterFactory( RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit.create(MainService.class);
    }




}
